package com.sales.service.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Environment;

/**
 * This class is used as a tool to get the entity content.
 * 
 * @author Sega
 *
 */
public class Utility {

	/**
	 * Method to write the entity as a file and to get the content of it.
	 * 
	 * @param response - inputstream is the input.
	 * @param name - file name to write.
	 * @return - String of entity content.
	 * @throws IOException
	 */
	public static String WiteFiletosd(InputStream response, String name) throws IOException {
		
		BufferedReader bReader = new BufferedReader(new InputStreamReader(response));
		StringBuilder sBuilder = new StringBuilder();
		String line = "";
		while ((line = bReader.readLine()) != null) {
			sBuilder.append(line);
		}
		
		File sdCard = Environment.getExternalStorageDirectory();
		File myFile = new File(sdCard, name);
		
		myFile.createNewFile();
		FileOutputStream fOut = new FileOutputStream(myFile);
		OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

		myOutWriter.write(sBuilder.toString());
		myOutWriter.close();
		fOut.close();
		
		return sBuilder.toString();
	}

}